#ifndef WIDGET_H
#define WIDGET_H

#include <QDebug>
#include <QFile>

#include <QMessageBox>
#include <QPalette>
#include <QPushButton>

#include <QSettings>
#include <QStackedWidget>

#include <QUdpSocket>
#include <QWidget>

#include "alsa.h"
#include "alsaty.h"
#include "alsatool.h"
#include "dcuinformation.h"

#include "equipmentstatus.h"

#include "specialgb.h"

#include "volumeset.h"
#include "gpio.h"
#include "selectzoneui.h"
#include "loginui.h"
#include "funselectui.h"
#include "localhostinfo.h"
#include "setting.h"
#include "updateview.h"
#include "http.h"

#define myCout()                                                   \
    qDebug() << "[" << __FILE__ << __FUNCTION__ << __LINE__ << "]" \
             << ":"

#define LOCTION QString("[ %1 %2 %3 ]").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__)

#define GetBit(x, y) ((x >> y) & 0x01)

#define maxAudioShowNum 15

typedef enum
{
    IDLE = 0,
    Call,
    Com,
    Hang,
    ComFlag  //连挂时有报警器接听标志位
} InterComStatusDef;

enum task_ID
{
    NOTASK = 0,
    AUTO,
    OCC,
    EMERGENCY,
    MEDIA,
    MAN,
    SJDJ,
    CKBJ,
    MAN_OTHER,  //对端人工
    INGB,
    OUTGB,
    ZBQH  //主备切换
};

typedef QList<QPushButton *> QButtonList;

enum MsgType
{
    MVB,
    OriginalMVBData,
    sendMVB,
    Alarm,
    OPERATION,
    Universal
};

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

    void InitSpecialGBWidget(void);

    void ReadINI(void);
    void Key_Init(void);

    void Key_ManGB(void);

    void GPIOInit(void);

    void msleep(int msec);
    void Dog_CLR(void);
    void MAN_CLR(void);

    void VolumeInit(void);
    void BD3812WriteVolume(unsigned char ch1volume, unsigned char ch2volume, unsigned char adrs);
    void AlsaCaptureVolume(QString element, int value);
    void AlsaPlaybackVolume(QString element, int value);
    void SelectChannel(int i);
    void initTopLabel();
    void initUI();

    QString getDevName(int type);

    void syncDateTime(QString dateTime);

    void writeLog(MsgType type, QString msg, bool isWrite = false, bool timeSync = false);

    void NoBlockDelayms(int ms);

    QString logLocation(QString loc, QString msg);

    void getNetIsOnline();

    void addGroupByFindDev(QMap<int, QString> groupMap, QStringList &devList, QList<int> list);

    void addPacketForJson(int cmdCode, int volume, int count, QString fileId, QStringList devIdList);

    void refreshView(QStringList devList, QStringList speakerList, QStringList fileNames);

    void getMapTable(QMap<QString, QString> &map);

    void dealCmdCode_1(QJsonObject rootObj);

    void dealCmdCode_9(QJsonObject rootObj);

    void dealCmdCode_11(QJsonObject rootObj);

    void dealCmdCode_33(QJsonObject rootObj);

    void dealCmdCode_34(QJsonObject rootObj);

    void msgBoxShow(QString msg);

    void msgBoxClose();

    void setAudioList(QStringList fileNameList, int page);

    QString readFileMd5(QString filePath);

    int8_t getCpuUsage();

    int8_t memoryShare();

    void setVolumeByJson();

private:
    QTimer *timerkey;

    QLabel *ProjectLabel;

    QPalette background;

    QLabel *netModeText;
    QLabel *netModeImage;
    QLabel *LineNameL;
    QLabel *dateTimeText;

    uint8_t devStatePage    = 0;
    uint8_t devStateMaxPage = 0;

    uint8_t zonePage      = 0;
    uint8_t devStateClear = 10;

    uint8_t specialGbPage    = 0;
    uint8_t specialGbMaxPage = 0;

    QMap<QString, QString> devIdMap;

    uint8_t devUpdateState = 0;
    QString md5Sum;

    double m_cpu_total__ = 0;
    double m_cpu_use__   = 0;
    int    m_cpuRate     = 0;
    int    m_memory      = 0;

    LoginUI *      loginUI;
    FunSelectUI *  funSelectUI;
    VolumeSet *    VolumeSetUI;
    SpecialGB *    SpecialGBUI;
    SelectZoneUI * selectZoneUI;
    LocalHostInfo *localHostInfo;
    Setting *      settingUI;
    UpdateView *   updateViewUI;
    Http *         http;

    EquipmentStatus *EquipmentStatusUI;

    QStackedWidget *StackedWidget;

    QSettings *BasicIniRead;
    int        SelectStation;

    QUdpSocket *udpSendSocket;
    QUdpSocket *udpRecSocket;

    InterComStatusDef MANGBStatus;

    QMessageBox   msgBox;
    unsigned char specialgb_id;

    bool update_equipment_status;
    bool update_alarm_status;

    ReadAudioThread   producer;
    WriteAudioThread  writeaudio;
    WriteAudioThread *writeAudioThread;

    QButtonList SpecialGBBtnList;

    QTimer *Lifetimer;
    QTimer *refreshViewTimer;

    QStringList devList;    //设备信息列表
    QStringList devIdList;  //设备Id列表
    QStringList speakerList;
    QStringList fileNameList;

    int m_emergencyIndex = 1;
    int m_crashIndex     = 1;

    GPIO *Beep;
    GPIO *GBLED;
    GPIO *COM1LED;
    GPIO *COM2LED;
    GPIO *AlarmLED;
    GPIO *TalkLED;
    GPIO *MasterLED;
    GPIO *MICSelect;

    GPIO *Dog;
    GPIO *AMC;
    GPIO *KeyMaster;
    GPIO *KeyTalk;
    GPIO *KeyAlarm;
    GPIO *KeyMan;
    GPIO *KeyHook;

    GPIO *A4052, *B4052;
    GPIO *Mute1, *Mute2;
    GPIO *SDA3812, *SCL3812;

private slots:

    void CurrentSelectSpecialGB();

    void SetSpecialGB();

    void showDefaultUI();

    void devStateUISlotforUp();

    void devStateUISlotforDown();

    void specialGbUISlotforUp();

    void specialGbUISlotforDown();

    void RecvData();

    void keyEvent();

    void life_status_send();

    void addPacketLifeTime();

    void showDateTime();

    void refreshViewTimerDown();

    void closemesbox(void);

    void setAlsaErrRecord(QString);

    void setVolumeRecord(QString);

    void pageInfoDone(QString);

    void emergencyInfo(int, int);

    void zonePageInfoDone(uint8_t);

    void playCmdDone(QStringList, QString, QString);

    void monitorOpenCmdDone(QString, QString);

    void stopGBcheck();

    void httpdownloadFileFinished();
};

#endif  // WIDGET_H
